'use strict';
const os = require('os');

let tool = {
    systemInfo:function(){
        let free = os.freemem(),total= os.totalmem();
        
        let sys = {
            uptime:tool.secondFormat(os.uptime()),
            loadavg:os.loadavg(),
            mem:{
                free:tool.byteFormat(free) + 'MB',
                used:tool.byteFormat(total - free) + 'MB',
                totalmem:tool.byteFormat(total) + 'MB'
            }
        };
        return sys;
    },
    byteFormat:function(byte){
        return parseInt(byte/1024/1024)
    },
    secondFormat:function(sec){
        if(!sec || sec < 1) return {value:sec};
        let days = parseInt(sec / 3600 / 24);
        sec = sec - (3600 * 24 * days);
        let hours =  parseInt(sec / 3600);
        sec = sec - (3600 * hours);
        let minutes =  parseInt(sec / 60);
        sec = sec - (60 * minutes);
        let str = days > 0 ? days + ' Days ' : '';
        str += str.length >0 || hours > 0 ? hours + ' Hours ' : '';
        str += str.length >0 || minutes > 0 ? minutes + ' Minutes ' : '';
        return {
            value:sec,
            days:days,hours:hours,minutes:minutes,seconds:sec,text:str + sec + ' Seconds'
        };
    }
};

module.exports = tool;